package com.singzi.leetcode.jianzhiOffer2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class T0074 {
}
class Solution0074 {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (o1, o2) -> o1[0] != o2[0]?o1[0] -o2[0]:o1[1] - o2[1]);
        List<int []>res = new ArrayList<>();

        int []temp = intervals[0];
        for(int i = 1;i < intervals.length;i++){
            if(intervals[i][0] >= temp[1]){
                res.add(temp);
            }else {
                temp[1] = Math.max(temp[1], intervals[i][1]);
            }
        }
        int [][]ans = new int[res.size()][2];
        for(int i = 0;i < res.size();i++){
            System.arraycopy(res.get(i), 0, ans[i], 0, 2);
        }
        return ans;
    }
}